package com.foodcommunity.service.impl;

import com.foodcommunity.dto.PostCreateDTO;
import com.foodcommunity.entity.FoodPost;
import com.foodcommunity.entity.User;
import com.foodcommunity.mapper.FoodPostMapper;
import com.foodcommunity.service.PostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.foodcommunity.mapper.PostTagMapper;
import com.foodcommunity.entity.PostTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.foodcommunity.vo.PostVO;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private FoodPostMapper postMapper;

    @Autowired
    private PostTagMapper postTagMapper;

    @Override
    @Transactional
    public FoodPost createPost(PostCreateDTO postDTO) {
        // 获取当前用户ID
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            throw new RuntimeException("用户未登录");
        }
        
        // 1. 创建文章实体
        FoodPost post = new FoodPost();
        post.setUserId(user.getId());
        post.setTitle(postDTO.getTitle());
        post.setContent(postDTO.getContent());
        post.setCategoryId(postDTO.getCategoryId());
        
        // 处理图片
        if (postDTO.getImages() != null && !postDTO.getImages().isEmpty()) {
            post.setImages(String.join(",", postDTO.getImages()));
        } else {
            post.setImages("");
        }
        
        post.setVideoUrl(postDTO.getVideoUrl());
        post.setIsOriginal(postDTO.getIsOriginal());
        post.setIsTop(postDTO.getIsTop());
        post.setStatus((byte) 1); // 1-已发布
        post.setLikes(0);
        post.setViews(0);
        post.setCreateTime(LocalDateTime.now());
        post.setUpdateTime(LocalDateTime.now());

        // 2. 保存文章
        postMapper.insert(post);

        // 3. 保存文章标签关联
        if (postDTO.getTagIds() != null && !postDTO.getTagIds().isEmpty()) {
            List<PostTag> postTags = postDTO.getTagIds().stream()
                .map(tagId -> {
                    PostTag postTag = new PostTag();
                    postTag.setPostId(post.getId());
                    postTag.setTagId(tagId);
                    return postTag;
                }).collect(Collectors.toList());
            postTagMapper.batchInsert(postTags);
        }

        return post;
    }

    @Override
    public List<PostVO> getPostList() {
        return postMapper.selectPostList();
    }
} 